package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDto;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.IntStream;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private CheckGroupService checkGroupService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void saveOrUpdateSetmealAndGroup(SetmealDto setmealDto) {
        Integer sid = setmealDto.getId();
        saveOrUpdate(setmealDto);
        if(StringUtils.isEmpty(sid)) {
            sid = setmealDto.getId();
        }else {
            baseMapper.deleteSetmealAndGroupById(sid);
        }
        Integer[] groupIds = setmealDto.getCheckgroupIds();
        for (Integer gid : groupIds) {
            baseMapper.addSetmealAndGroup(sid,gid);
        }
        if(setmealDto.getImg()!=null) {
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmealDto.getImg());
        }
    }

    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        queryWrapper.like("name",queryString==null?"":queryString).or()
                .like("code",queryString==null?"":queryString).or()
                .like("helpCode",queryString==null?"":queryString);
        Page<Setmeal> page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize()),queryWrapper);
        List<Setmeal> setmealList = page.getRecords();
        long total = page.getTotal();
        Map map = new HashMap();
        map.put("list",setmealList);
        map.put("total",total);
        return map;
    }

    @Override
    public Map findGroupIdAndImgUrlById(Integer id) {
        List<CheckGroup> list = checkGroupService.list();
        int[] gids = baseMapper.findGroupId(id);
        String url = baseMapper.findImgUrl(id);
        Map map = new HashMap();
        map.put("list",list);
        map.put("gids", gids);
        map.put("url",url);
        return map;
    }

    @Override
    public SetmealVO findSetmealDetail(int id) {
        SetmealVO setmealVO = baseMapper.findSetmealById(id);
        List<CheckGroupVO> checkGroups = baseMapper.findCheckgroupsById(id);
        for (CheckGroupVO checkGroupVO : checkGroups) {
            List<CheckItem> checkItems = checkGroupService.findCheckItemsById(checkGroupVO.getId());
            checkGroupVO.setCheckItems(checkItems);
        }
        setmealVO.setCheckGroups(checkGroups);
        return setmealVO;
    }
}
